package cn.itcast.waterboss.dao;

import java.sql.SQLException;

import cn.itcast.waterboss.entity.Owners;
import oracle.jdbc.OracleTypes;

/**
 * jdbc操作存储过程
 * @author smile
 *
 */
public class OwnerProDao {
	
	/**
	 * 增加（不带传出参数）
	 * 
	 * @param owners
	 */
	public static void add(Owners owners) {

		java.sql.Connection conn = null;
		java.sql.CallableStatement stmt = null;

		try {
			conn = BaseDao.getConnect();
			// 语句的写法与普通的sql语句不同
			stmt = conn.prepareCall("{call pro_owners_add(?,?,?,?,?)}");

			stmt.setString(1, owners.getName());
			stmt.setLong(2, owners.getAddress());
			stmt.setString(3, owners.getHouseNumber());
			stmt.setString(4, owners.getWaterMeter());
			stmt.setLong(5, owners.getOwnerTypeId());

			stmt.execute();
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			BaseDao.close(null, stmt, conn);
		}
	}
	
	/**
	 * 增加 （带有传出参数）
	 * @param owners
	 */
	public static long addOut(Owners owners){
		
		java.sql.Connection conn = null;
		java.sql.CallableStatement stmt = null;
		long id = 0;
		
		try {
			conn=BaseDao.getConnect();
			stmt=conn.prepareCall("{call pro_owners_add(?,?,?,?,?,?)}");
			stmt.setString(1, owners.getName());
			stmt.setLong(2, owners.getAddress());
			stmt.setString(3, owners.getHouseNumber());
			stmt.setString(4, owners.getWaterMeter());			
			stmt.setLong(5, owners.getOwnerTypeId());
			
			// 注册传出参数类型
			stmt.registerOutParameter(6, OracleTypes.NUMBER);
			
			stmt.execute();			
			
			// 提取传出参数
			id = stmt.getLong(6);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			BaseDao.close(null, stmt, conn);
		}	
		return id;
	}

}
